//
//  ImagesView.m
//  仿微信朋友圈
//
//  Created by dllo on 16/9/3.
//  Copyright © 2016年 DM.C. All rights reserved.
//

#import "ImagesView.h"
#import "SDAutoLayout.h"
#import "UIView+Frame.h"
#import "UIImageView+WebCache.h"

@interface ImagesView ()

@end

@implementation ImagesView
{
    UIImageView *firstImage;
    UIImageView *twoImage;
    UIImageView *threeImage;
    UIImageView *fourImage;
    UIImageView *fiveImage;
    UIImageView *sixImage;
    UIImageView *sevenImage;
    UIImageView *eightImage;
    UIImageView *nineImage;
    
}
- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self createImages];
    }
    return self;
}

- (void)setImagesArr:(NSMutableArray *)imagesArr {
    _imagesArr = imagesArr;
    
    
    [self createImages];
    
}
- (void)createImages {
   
    // 创建imageView
    firstImage = [UIImageView new];
    twoImage = [UIImageView new];
    threeImage = [UIImageView new];
    fourImage = [UIImageView new];
    fiveImage = [UIImageView new];
    sixImage = [UIImageView new];
    sevenImage = [UIImageView new];
    eightImage = [UIImageView new];
    nineImage = [UIImageView new];
    firstImage.contentMode = UIViewContentModeScaleAspectFill;
    firstImage.clipsToBounds = YES;
    twoImage.contentMode = UIViewContentModeScaleAspectFill;
    twoImage.clipsToBounds = YES;
    threeImage.contentMode = UIViewContentModeScaleAspectFill;
    threeImage.clipsToBounds = YES;
    fourImage.contentMode = UIViewContentModeScaleAspectFill;
    fourImage.clipsToBounds = YES;
    fiveImage.contentMode = UIViewContentModeScaleAspectFill;
    fiveImage.clipsToBounds = YES;
    sixImage.contentMode = UIViewContentModeScaleAspectFill;
    sixImage.clipsToBounds = YES;
    sevenImage.contentMode = UIViewContentModeScaleAspectFill;
    sevenImage.clipsToBounds = YES;
    eightImage.contentMode = UIViewContentModeScaleAspectFill;
    eightImage.clipsToBounds = YES;
    nineImage.contentMode = UIViewContentModeScaleAspectFill;
    nineImage.clipsToBounds = YES;

    [self addSubview:firstImage];
    [self addSubview:twoImage];
    [self addSubview:threeImage];
    [self addSubview:fourImage];
    [self addSubview:fiveImage];
    [self addSubview:sixImage];
    [self addSubview:sevenImage];
    [self addSubview:eightImage];
    [self addSubview:nineImage];
    
    for (int i = 0; i < _imagesArr.count; i++) {
        
        if (_imagesArr.count == 1) {
            [firstImage sd_setImageWithURL:[NSURL URLWithString:_imagesArr[i]] placeholderImage:[UIImage imageNamed:@"project_bg_detail"]];
            firstImage.sd_layout
            .topSpaceToView(self, 20)
            .leftEqualToView(self)
            .rightEqualToView(self)
            .autoHeightRatio(0.6);
            [self setupAutoHeightWithBottomView:firstImage bottomMargin:10];
        } else if (_imagesArr.count == 2) {
            
            if (i == 0) {
                [firstImage sd_setImageWithURL:[NSURL URLWithString:_imagesArr[i]] placeholderImage:[UIImage imageNamed:@"project_bg_detail"]];
                firstImage.sd_layout
                .topSpaceToView(self, 20)
                .leftEqualToView(self)
                .autoHeightRatio(1);
                
            } else if (i == 1) {
                [twoImage sd_setImageWithURL:[NSURL URLWithString:_imagesArr[i]] placeholderImage:[UIImage imageNamed:@"project_bg_detail"]];
                twoImage.sd_layout
                .topSpaceToView(self, 20)
                .rightEqualToView(self)
                .autoHeightRatio(1);
            }
            [self setupAutoWidthFlowItems:@[firstImage, twoImage] withPerRowItemsCount:2 verticalMargin:0 horizontalMargin:5 verticalEdgeInset:20 horizontalEdgeInset:0];
            [self setupAutoHeightWithBottomView:firstImage bottomMargin:10];
            
        } else if (_imagesArr.count == 3) {
            if (i == 0) {
                [firstImage sd_setImageWithURL:[NSURL URLWithString:_imagesArr[i]] placeholderImage:[UIImage imageNamed:@"project_bg_detail"]];
                firstImage.sd_layout
                .topSpaceToView(self, 20)
                .leftEqualToView(self)
                .widthRatioToView(self, 2 / 3.0)
                .autoHeightRatio(1);
               
            } else if (i == 1) {
                [twoImage sd_setImageWithURL:[NSURL URLWithString:_imagesArr[i]] placeholderImage:[UIImage imageNamed:@"project_bg_detail"]];
                twoImage.sd_layout
                .topSpaceToView(self, 20)
                .rightSpaceToView(self, -1.5)
                .leftSpaceToView(firstImage, 5)
                .autoHeightRatio(1);
                
            } else if (i == 2) {
                [threeImage sd_setImageWithURL:[NSURL URLWithString:_imagesArr[i]] placeholderImage:[UIImage imageNamed:@"project_bg_detail"]];
                threeImage.sd_layout
                .topSpaceToView(twoImage, 5)
                .leftSpaceToView(firstImage, 5)
                .widthRatioToView(twoImage, 1)
                .autoHeightRatio(1);
                
            }
            [self setupAutoHeightWithBottomView:firstImage bottomMargin:10];
        } else if (_imagesArr.count == 4) {
            if (i == 0) {
                [firstImage sd_setImageWithURL:[NSURL URLWithString:_imagesArr[i]] placeholderImage:[UIImage imageNamed:@"project_bg_detail"]];
                firstImage.sd_layout
                .topSpaceToView(self, 20)
                .leftEqualToView(self)
                .rightEqualToView(self)
                .autoHeightRatio(0.5);
                
            } else if (i == 1) {
                [twoImage sd_setImageWithURL:[NSURL URLWithString:_imagesArr[i]] placeholderImage:[UIImage imageNamed:@"project_bg_detail"]];
                twoImage.sd_layout
                .topSpaceToView(firstImage, 5)
                .leftEqualToView(self)
                .widthRatioToView(self, 394 / 3.0 / 404.0)
                .autoHeightRatio(1);
                
            } else if (i == 2) {
                [threeImage sd_setImageWithURL:[NSURL URLWithString:_imagesArr[i]] placeholderImage:[UIImage imageNamed:@"project_bg_detail"]];
                threeImage.sd_layout
                .topSpaceToView(firstImage, 5)
                .leftSpaceToView(twoImage, 5)
                .widthRatioToView(twoImage, 1)
                .autoHeightRatio(1);
                
            } else if (i == 3) {
                [fourImage sd_setImageWithURL:[NSURL URLWithString:_imagesArr[i]] placeholderImage:[UIImage imageNamed:@"project_bg_detail"]];
                fourImage.sd_layout
                .topSpaceToView(firstImage, 5)
                .leftSpaceToView(threeImage, 5)
                .widthRatioToView(twoImage, 1)
                .autoHeightRatio(1);
            }
            [self setupAutoHeightWithBottomView:fourImage bottomMargin:10];
        } else if (_imagesArr.count == 5) {
            if (i == 0) {
                [firstImage sd_setImageWithURL:[NSURL URLWithString:_imagesArr[i]] placeholderImage:[UIImage imageNamed:@"project_bg_detail"]];
                firstImage.sd_layout
                .topSpaceToView(self, 20)
                .leftEqualToView(self)
                .widthRatioToView(self, 2 / 3.0)
                .autoHeightRatio(1);
                
            } else if (i == 1) {
                [twoImage sd_setImageWithURL:[NSURL URLWithString:_imagesArr[i]] placeholderImage:[UIImage imageNamed:@"project_bg_detail"]];
                twoImage.sd_layout
                .topSpaceToView(self, 20)
                .rightSpaceToView(self, -1.5)
                .leftSpaceToView(firstImage, 5)
                .autoHeightRatio(1);
                
            } else if (i == 2) {
                [threeImage sd_setImageWithURL:[NSURL URLWithString:_imagesArr[i]] placeholderImage:[UIImage imageNamed:@"project_bg_detail"]];
                threeImage.sd_layout
                .topSpaceToView(twoImage, 5)
                .leftSpaceToView(firstImage, 5)
                .widthRatioToView(twoImage, 1)
                .autoHeightRatio(1);
                
            } else if (i == 3) {
                [fourImage sd_setImageWithURL:[NSURL URLWithString:_imagesArr[i]] placeholderImage:[UIImage imageNamed:@"project_bg_detail"]];
                fourImage.sd_layout
                .topSpaceToView(firstImage, 5)
                .widthRatioToView(self, 399 / 404.0 / 2.0)
                .leftEqualToView(self)
                .autoHeightRatio(0.6);
            } else if (i == 4) {
                [fiveImage sd_setImageWithURL:[NSURL URLWithString:_imagesArr[i]] placeholderImage:[UIImage imageNamed:@"project_bg_detail"]];
                fiveImage.sd_layout
                .centerYEqualToView(fourImage)
                .leftSpaceToView(fourImage, 5)
                .widthRatioToView(fourImage, 1)
                .autoHeightRatio(0.6);
            }
            [self setupAutoHeightWithBottomView:fourImage bottomMargin:10];

            
        } else if (_imagesArr.count == 6) {
            if (i == 0) {
                [firstImage sd_setImageWithURL:[NSURL URLWithString:_imagesArr[i]] placeholderImage:[UIImage imageNamed:@"project_bg_detail"]];
                firstImage.sd_layout
                .topSpaceToView(self, 20)
                .leftEqualToView(self)
                .widthRatioToView(self, 2 / 3.0)
                .autoHeightRatio(1);
                
            } else if (i == 1) {
                [twoImage sd_setImageWithURL:[NSURL URLWithString:_imagesArr[i]] placeholderImage:[UIImage imageNamed:@"project_bg_detail"]];
                twoImage.sd_layout
                .topSpaceToView(self, 20)
                .rightSpaceToView(self, -1.5)
                .leftSpaceToView(firstImage, 5)
                .autoHeightRatio(1);
                
            } else if (i == 2) {
                [threeImage sd_setImageWithURL:[NSURL URLWithString:_imagesArr[i]] placeholderImage:[UIImage imageNamed:@"project_bg_detail"]];
                threeImage.sd_layout
                .topSpaceToView(twoImage, 5)
                .leftSpaceToView(firstImage, 5)
                .widthRatioToView(twoImage, 1)
                .autoHeightRatio(1);
                
            } else if (i == 3) {
                [fourImage sd_setImageWithURL:[NSURL URLWithString:_imagesArr[i]] placeholderImage:[UIImage imageNamed:@"project_bg_detail"]];
                fourImage.sd_layout
                .topSpaceToView(firstImage, 5)
                .leftEqualToView(self)
                .widthRatioToView(self, 394 / 404.0 / 3.0)
                .autoHeightRatio(1);
            } else if (i == 4) {
                [fiveImage sd_setImageWithURL:[NSURL URLWithString:_imagesArr[i]] placeholderImage:[UIImage imageNamed:@"project_bg_detail"]];
                fiveImage.sd_layout
                .centerYEqualToView(fourImage)
                .leftSpaceToView(fourImage, 5)
                .widthRatioToView(fourImage, 1)
                .autoHeightRatio(1);
            } else if (i == 5) {
                [sixImage sd_setImageWithURL:[NSURL URLWithString:_imagesArr[i]] placeholderImage:[UIImage imageNamed:@"project_bg_detail"]];
                sixImage.sd_layout
                .centerYEqualToView(fiveImage)
                .leftSpaceToView(fiveImage, 5)
                .widthRatioToView(fourImage, 1)
                .autoHeightRatio(1);
            }
            [self setupAutoHeightWithBottomView:fourImage bottomMargin:10];

            
        } else if (_imagesArr.count == 7) {
            if (i == 0) {
                [firstImage sd_setImageWithURL:[NSURL URLWithString:_imagesArr[i]] placeholderImage:[UIImage imageNamed:@"project_bg_detail"]];
                firstImage.sd_layout
                .topSpaceToView(self, 20)
                .leftEqualToView(self)
                .widthRatioToView(self, 2 / 3.0)
                .autoHeightRatio(1);
                
            } else if (i == 1) {
                [twoImage sd_setImageWithURL:[NSURL URLWithString:_imagesArr[i]] placeholderImage:[UIImage imageNamed:@"project_bg_detail"]];
                twoImage.sd_layout
                .topSpaceToView(self, 20)
                .rightSpaceToView(self, -1.5)
                .leftSpaceToView(firstImage, 5)
                .autoHeightRatio(1);
                
            } else if (i == 2) {
                [threeImage sd_setImageWithURL:[NSURL URLWithString:_imagesArr[i]] placeholderImage:[UIImage imageNamed:@"project_bg_detail"]];
                threeImage.sd_layout
                .topSpaceToView(twoImage, 5)
                .leftSpaceToView(firstImage, 5)
                .widthRatioToView(twoImage, 1)
                .autoHeightRatio(1);
                
            } else if (i == 3) {
                [fourImage sd_setImageWithURL:[NSURL URLWithString:_imagesArr[i]] placeholderImage:[UIImage imageNamed:@"project_bg_detail"]];
                fourImage.sd_layout
                .topSpaceToView(firstImage, 5)
                .leftEqualToView(self)
                .widthRatioToView(self, 389 / 404.0 / 4.0)
                .autoHeightRatio(1);
            } else if (i == 4) {
                [fiveImage sd_setImageWithURL:[NSURL URLWithString:_imagesArr[i]] placeholderImage:[UIImage imageNamed:@"project_bg_detail"]];
                fiveImage.sd_layout
                .centerYEqualToView(fourImage)
                .leftSpaceToView(fourImage, 5)
                .widthRatioToView(fourImage, 1)
                .autoHeightRatio(1);
            } else if (i == 5) {
                [sixImage sd_setImageWithURL:[NSURL URLWithString:_imagesArr[i]] placeholderImage:[UIImage imageNamed:@"project_bg_detail"]];
                sixImage.sd_layout
                .centerYEqualToView(fiveImage)
                .leftSpaceToView(fiveImage, 5)
                .widthRatioToView(fourImage, 1)
                .autoHeightRatio(1);
            } else if (i == 6) {
                [sevenImage sd_setImageWithURL:[NSURL URLWithString:_imagesArr[i]] placeholderImage:[UIImage imageNamed:@"project_bg_detail"]];
                sevenImage.sd_layout
                .centerYEqualToView(sixImage)
                .leftSpaceToView(sixImage, 5)
                .widthRatioToView(fourImage, 1)
                .autoHeightRatio(1);
            }
            
            [self setupAutoHeightWithBottomView:fourImage bottomMargin:10];
            
        } else if (_imagesArr.count == 8) {
            if (i == 0) {
                [firstImage sd_setImageWithURL:[NSURL URLWithString:_imagesArr[i]] placeholderImage:[UIImage imageNamed:@"project_bg_detail"]];
                firstImage.sd_layout
                .topSpaceToView(self, 20)
                .leftEqualToView(self)
                .widthRatioToView(self, 389 / 404.0 / 4.0)
                .autoHeightRatio(1);
                
            } else if (i == 1) {
                [twoImage sd_setImageWithURL:[NSURL URLWithString:_imagesArr[i]] placeholderImage:[UIImage imageNamed:@"project_bg_detail"]];
                twoImage.sd_layout
                .topSpaceToView(self, 20)
                .leftSpaceToView(firstImage, 5)
                .widthRatioToView(firstImage, 1)
                .autoHeightRatio(1);
            } else if (i == 2) {
                [threeImage sd_setImageWithURL:[NSURL URLWithString:_imagesArr[i]] placeholderImage:[UIImage imageNamed:@"project_bg_detail"]];
                threeImage.sd_layout
                .topSpaceToView(self, 20)
                .leftSpaceToView(twoImage, 5)
                .widthRatioToView(twoImage, 1)
                .autoHeightRatio(1);
                
            } else if (i == 3) {
                [fourImage sd_setImageWithURL:[NSURL URLWithString:_imagesArr[i]] placeholderImage:[UIImage imageNamed:@"project_bg_detail"]];
                fourImage.sd_layout
                .topSpaceToView(self, 20)
                .leftSpaceToView(threeImage, 5)
                .widthRatioToView(twoImage, 1)
                .autoHeightRatio(1);
                
            } else if (i == 4) {
                [fiveImage sd_setImageWithURL:[NSURL URLWithString:_imagesArr[i]] placeholderImage:[UIImage imageNamed:@"project_bg_detail"]];
                fiveImage.sd_layout
                .topSpaceToView(firstImage, 5)
                .leftEqualToView(self)
                .widthRatioToView(fourImage, 1)
                .autoHeightRatio(1);
            } else if (i == 5) {
                [sixImage sd_setImageWithURL:[NSURL URLWithString:_imagesArr[i]] placeholderImage:[UIImage imageNamed:@"project_bg_detail"]];
                sixImage.sd_layout
                .centerYEqualToView(fiveImage)
                .leftSpaceToView(fiveImage, 5)
                .widthRatioToView(fourImage, 1)
                .autoHeightRatio(1);
            } else if (i == 6) {
                [sevenImage sd_setImageWithURL:[NSURL URLWithString:_imagesArr[i]] placeholderImage:[UIImage imageNamed:@"project_bg_detail"]];
                sevenImage.sd_layout
                .centerYEqualToView(fiveImage)
                .leftSpaceToView(sixImage, 5)
                .widthRatioToView(fourImage, 1)
                .autoHeightRatio(1);
            } else if (i == 7) {
                [eightImage sd_setImageWithURL:[NSURL URLWithString:_imagesArr[i]] placeholderImage:[UIImage imageNamed:@"project_bg_detail"]];
                eightImage.sd_layout
                .centerYEqualToView(fiveImage)
                .leftSpaceToView(sevenImage, 5)
                .widthRatioToView(fourImage, 1)
                .autoHeightRatio(1);
            }
            [self setupAutoHeightWithBottomView:fiveImage bottomMargin:10];
        } else if (_imagesArr.count == 9) {
            if (i == 0) {
                [firstImage sd_setImageWithURL:[NSURL URLWithString:_imagesArr[i]] placeholderImage:[UIImage imageNamed:@"project_bg_detail"]];
                firstImage.sd_layout
                .topSpaceToView(self, 20)
                .widthRatioToView(self, 394 / 404.0 / 3.0)
                .leftEqualToView(self)
                .autoHeightRatio(1);
                
            } else if (i == 1) {
                [twoImage sd_setImageWithURL:[NSURL URLWithString:_imagesArr[i]] placeholderImage:[UIImage imageNamed:@"project_bg_detail"]];
                twoImage.sd_layout
                .topSpaceToView(self, 20)
                .leftSpaceToView(firstImage, 5)
                .widthRatioToView(firstImage, 1)
                .autoHeightRatio(1);
            } else if (i == 2) {
                [threeImage sd_setImageWithURL:[NSURL URLWithString:_imagesArr[i]] placeholderImage:[UIImage imageNamed:@"project_bg_detail"]];
                threeImage.sd_layout
                .topSpaceToView(self, 20)
                .leftSpaceToView(twoImage, 5)
                .widthRatioToView(twoImage, 1)
                .autoHeightRatio(1);
                
            } else if (i == 3) {
                [fourImage sd_setImageWithURL:[NSURL URLWithString:_imagesArr[i]] placeholderImage:[UIImage imageNamed:@"project_bg_detail"]];
                fourImage.sd_layout
                .topSpaceToView(firstImage, 5)
                .leftEqualToView(self)
                .widthRatioToView(firstImage, 1)
                .autoHeightRatio(1);
            } else if (i == 4) {
                [fiveImage sd_setImageWithURL:[NSURL URLWithString:_imagesArr[i]] placeholderImage:[UIImage imageNamed:@"project_bg_detail"]];
                fiveImage.sd_layout
                .centerYEqualToView(fourImage)
                .leftSpaceToView(fourImage, 5)
                .widthRatioToView(fourImage, 1)
                .autoHeightRatio(1);
            } else if (i == 5) {
                [sixImage sd_setImageWithURL:[NSURL URLWithString:_imagesArr[i]] placeholderImage:[UIImage imageNamed:@"project_bg_detail"]];
                sixImage.sd_layout
                .centerYEqualToView(fiveImage)
                .leftSpaceToView(fiveImage, 5)
                .widthRatioToView(fourImage, 1)
                .autoHeightRatio(1);
            } else if (i == 6) {
                [sevenImage sd_setImageWithURL:[NSURL URLWithString:_imagesArr[i]] placeholderImage:[UIImage imageNamed:@"project_bg_detail"]];
                sevenImage.sd_layout
                .centerXEqualToView(firstImage)
                .topSpaceToView(fourImage, 5)
                .widthRatioToView(fourImage, 1)
                .autoHeightRatio(1);
            } else if (i == 7) {
                [eightImage sd_setImageWithURL:[NSURL URLWithString:_imagesArr[i]] placeholderImage:[UIImage imageNamed:@"project_bg_detail"]];
                eightImage.sd_layout
                .centerYEqualToView(sevenImage)
                .leftSpaceToView(sevenImage, 5)
                .widthRatioToView(fourImage, 1)
                .autoHeightRatio(1);
            } else if (i == 8) {
                [nineImage sd_setImageWithURL:[NSURL URLWithString:_imagesArr[i]] placeholderImage:[UIImage imageNamed:@"project_bg_detail"]];
                nineImage.sd_layout
                .centerYEqualToView(sevenImage)
                .leftSpaceToView(eightImage, 5)
                .widthRatioToView(fourImage, 1)
                .autoHeightRatio(1);
            }
            [self setupAutoHeightWithBottomView:sevenImage bottomMargin:10];

        }
        
        //for循环的最后一个括号
    }

}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
